/*
 * Copyright (C) 2012 SeekDeepWithin.com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package com.seekdeepwithin;

import com.seekdeepwithin.Resources.*;
import java.io.*;
import javafx.application.Application;
import javafx.event.*;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.image.*;
import javafx.scene.layout.*;
import javafx.stage.*;

/**
 * User: Jonathan Montiverdi Date: 9/26/12
 *
 * Entry point for seek deep within.
 */
public class SeekDeepWithin extends Application
{
   /**
    * Starts the application.
    *
    * @param stage Main stage of application.
    * @throws Exception Any exception.
    */
   @Override
   public void start (final Stage stage) throws Exception
   {
      FXMLLoader loader = new FXMLLoader (getClass ().getResource ("Main.fxml"));
      final MainController controller = MainController.getInstance ();
      loader.setController (controller);
      BorderPane root = (BorderPane) loader.load ();
      stage.setOnCloseRequest (new EventHandler<WindowEvent> ()
      {
         @Override
         public void handle (WindowEvent t)
         {
            /* Disable window closing...
             * stage.setVisible(true);
             * t.consume();
             */
            try {
               controller.close ();
            } catch (IOException ex) {
               ex.printStackTrace (System.err);
            }
         }
      });

      stage.setOnShown (new EventHandler<WindowEvent> ()
      {
         @Override
         public void handle (WindowEvent t)
         {
            controller.start (stage);
         }
      });
      stage.getIcons ().add (new Image (SdwResources.SDW_ICON));
      stage.setTitle ("Seek Deep Within");
      Scene scene = new Scene (root, 1024, 768);
      scene.getStylesheets ().add (SdwResources.MAIN_CSS);
      stage.setScene (scene);
      stage.show ();
   }

   /**
    * The main() method is ignored in correctly deployed JavaFX application.
    * main() serves only as fallback in case the application can not be
    * launched through deployment artifacts, e.g., in IDEs with limited FX
    * support. NetBeans ignores main().
    *
    * @param args the command line arguments
    */
   public static void main (String[] args)
   {
      launch (args);
   }
}
